package com.jn.servicedemo1.views;

import android.animation.ObjectAnimator;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jn.servicedemo1.R;
import com.jn.servicedemo1.constant.Constants;
import com.jn.servicedemo1.service.CustomService;
import com.jn.servicedemo1.widget.CircleImageView;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mLast;
    private Button mPlay;
    private Button mNext;
    public CircleImageView mIvMusic;
    private boolean mPlaying = true;

    private MyServiceConnection mConnection;
    private CustomService mService;
    private ObjectAnimator animator;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        mLast = findViewById(R.id.last);
        mPlay = findViewById(R.id.play);
        mNext = findViewById(R.id.next);
        mIvMusic = findViewById(R.id.iv_music);
        mLast.setOnClickListener(this);
        mPlay.setOnClickListener(this);
        mNext.setOnClickListener(this);
        mConnection = new MyServiceConnection();
        Intent intent = new Intent(this, CustomService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
        animator = ObjectAnimator.ofFloat(mIvMusic,"rotation",0f,360.0f);
        animator.setDuration(10000);    //动画旋转一周的时间为10秒
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(-1);    //-1表示设置动画无限循环
        animator.start();
    }

    @Override
    public void onClick(View v) {
        sendCommand2(v.getId());
    }

    private void sendCommand(int id) {
        Intent intent = new Intent(this, CustomService.class);
        String command = null;
        if (id == R.id.last) {
            command = Constants.COMMAND_LAST;
        } else if (id == R.id.next) {
            command = Constants.COMMAND_NEXT;
        } else if (id == R.id.play) {
            if (mPlaying) {
                command = Constants.COMMAND_PAUSE;
                mPlay.setText("play");
                mPlaying = false;
            } else {
                command = Constants.COMMAND_PLAY;
                mPlay.setText("pause");
                mPlaying = true;
            }
        }
        intent.putExtra("command", command);
        startService(intent);
    }

    private void sendCommand2(int id) {
        if (mService == null) {
            return;
        }
        if (id == R.id.last) {
            mService.last();
        } else if (id == R.id.next) {
            mService.next();
        } else if (id == R.id.play) {
            if (mPlaying) {
                mService.pause();
                mPlay.setText("play");
                mPlaying = false;
                animator.pause();
            } else {
                mService.play();
                mPlay.setText("pause");
                mPlaying = true;
                animator.start();
            }
        }
    }

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ((CustomService.MyBinder) service).getService();
            mService.play();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    }
}

